package com.wtgroup.demo.common.util;

import cn.hutool.core.date.DateUtil;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/4/5 13:50
 */
public class Tools {

    public static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    /**
     * 异或交换
     *
     * Note: 需要确保 i != j .
     * 
     * @param arr
     * @param i
     * @param j
     */
    public static void swapXor(int[] arr, int i, int j) {
        // 异或交换
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
    }

    public static void printArray(int[] arr) {
        System.out.println(Arrays.toString(arr));
    }
    public static void printArray(String[] arr) {
        System.out.println(Arrays.toString(arr));
    }

    /**
     * int[] -> Integer[]
     */
    public static Integer[] int2IntegerArray(int[] arr) {
        Integer[] ret = new Integer[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i];
        }
        return ret;
    }

    public static int[] integer2IntArray(Integer[] arr) {
        int[] ret = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ret[i] = arr[i];
        }
        return ret;
    }

    /**
     * 比较两个数组是否相等
     * 
     * @param arr1
     * @param arr2
     * @return
     */
    public static boolean equals(int[] arr1, int[] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }

        return true;
    }

    public static boolean equals(Object[] arr1, Object[] arr2) {
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (Objects.equals(arr1[i], arr2[i]) == false) {
                return false;
            }
        }

        return true;
    }

    public static long time(Runnable runnable) {
        return time(null, runnable);
    }

    public static long time(String tag, Runnable runnable) {
        return time(tag, 1, runnable);
    }

    public static long time(String tag, int repeat, Runnable runnable) {
        long cost;
        long t0 = System.currentTimeMillis();
        try {
            for (int i = 0; i < repeat; i++) {
                runnable.run();
            }
        } finally {
            long t1 = System.currentTimeMillis();
            tag = tag == null ? "" : "[" + tag + "] ";
            cost = t1 - t0;
        }
        return cost;
    }

    public static long printTime(String tag, int repeat, Runnable runnable) {
        long cost = time(tag, repeat, runnable);
        System.out.println("==> " + tag + DateUtil.formatBetween(cost));
        return cost;
    }

    public static Integer max(Integer... nums) {
        Integer ret = null;
        for (Integer num : nums) {
            if (num == null) continue;
            if (ret == null) {
                ret = num;
            }
            else if (num > ret) {
                ret = num;
            }
        }

        return ret;
    }

    public static Integer min(Integer... nums) {
        Integer ret = null;
        for (Integer num : nums) {
            if (num == null) continue;
            if (ret == null) {
                ret = num;
            }
            else if (num < ret) {
                ret = num;
            }
        }

        return ret;
    }

}
